--[[
	Copyright (C) Udorn (Blackhand)
--]]

ltmstr = ltmstr or {}

lootlog.LootEntry = {}
lootlog.LootEntry.prototype = {}
lootlog.LootEntry.metatable = {__index = lootlog.LootEntry.prototype}

local log = lootlog.Debug:new("LootEntry")

local WIDTH = 200
local HEIGHT = 44
local ICON_WIDTH = 38
local ICON_HEIGHT = 38
local ICON_OFFSET = 3

local BACKGROUND_TEXTURE = "Interface/Tooltips/UI-Tooltip-Background" 
--local BACKGROUND_TEXTURE = "Interface/ItemTextFrame/ItemText-Stone-BotLeft"
--local BACKGROUND_TEXTURE = "Interface/BUTTONS/UI-Quickslot"


--local PLAYER_FONT = "SystemFont_Med2"
local PLAYER_FONT = "GameFontNormal"
local WINNER_FONT = "GameFontNormal"

local WHITE_FONT = "|cffffffff"
local COLOR_END = "|r"

local function _OnEnterItem(button)
	GameTooltip:SetOwner(button, "ANCHOR_RIGHT")
	if (button.obj.itemLink) then
		GameTooltip:SetOwner(button, "ANCHOR_RIGHT")
		GameTooltip:SetHyperlink(button.obj.itemLink)
	end
end

local function _SaveWindowPosition(self)
	if (not self.profile.windowPosition) then
		self.profile.windowPosition = {}
	end
	local save = self.profile.windowPosition
	local frame = self.frame:GetParent()
	local point, relativeTo, relativePoint, x, y = frame:GetPoint(1)
	save.point = point
	save.relativePoint = relativePoint
	save.x = math.floor(x + 0.5)
	save.y = math.floor(y + 0.5)
end

local function _StopMovingOrSizing(frame)
	local self = frame.obj
	self.frame:GetParent():StopMovingOrSizing()
	_SaveWindowPosition(self)
end

local function _StartMoving(frame)
	local self = frame.obj
	if (self.frame:GetParent():IsMovable()) then
		self.frame:GetParent():StartMoving() 
	end
end

local function _InitFrame(self, parent, idx)
	local frame = CreateFrame("Frame", "LootEntry"..idx, parent)
	frame.obj = self
	frame:SetWidth(WIDTH)
	frame:SetHeight(HEIGHT)
	frame:SetScript("OnMouseDown", _StartMoving)
	frame:SetScript("OnMouseUp", _StopMovingOrSizing)
--	local texture = frame:CreateTexture(nil, "BACKGROUND")
--	texture:SetAllPoints(frame)
--	texture:SetTexture(0, 0, 0, 1)
	frame:SetBackdrop({bgFile = BACKGROUND_TEXTURE, 
		edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
		tile = true, tileSize = 16, edgeSize = 16, 
		insets = { left = 4, right = 4, top = 4, bottom = 4 }});
	--frame:SetBackdropColor(1,1,1,1);
	frame:Show()
	
	local itemButton = CreateFrame("Button", nil, frame)
	itemButton:SetWidth(ICON_WIDTH)
	itemButton:SetHeight(ICON_HEIGHT)
	itemButton:SetPoint("TOPLEFT", ICON_OFFSET, -ICON_OFFSET)
	itemButton:SetScript("OnEnter", _OnEnterItem)
	itemButton:SetScript("OnLeave", function() GameTooltip:Hide(); end)
	itemButton.obj = self
	
	local playerText = frame:CreateFontString(nil, "ARTWORK", PLAYER_FONT)
	playerText:SetPoint("TOPLEFT", ICON_WIDTH + 6, -6)

	local winnerText = frame:CreateFontString(nil, "ARTWORK", WINNER_FONT)
	winnerText:SetPoint("BOTTOMLEFT", ICON_WIDTH + 6, 4)

	self.frame = frame
	self.itemButton = itemButton
	self.playerText = playerText
	self.winnerText = winnerText
	self.texture = texture
end

function lootlog.LootEntry:new(parent, idx)
	local instance = setmetatable({}, self.metatable)
	_InitFrame(instance, parent, idx)
	instance.profile = lootlog.LootLog.db.profile
	return instance
end

function lootlog.LootEntry.prototype:SetPoint(...)
	self.frame:SetPoint(...)
end

function lootlog.LootEntry.prototype:SetItem(itemLink)
	self.itemLink = itemLink
	self.itemButton:SetNormalTexture(GetItemIcon(itemLink))
end

function lootlog.LootEntry.prototype:SetPlayerText(text)
	self.playerText:SetText(WHITE_FONT..text..COLOR_END)
	return self.playerText:GetStringWidth()
end

function lootlog.LootEntry.prototype:SetWinnerText(text)
	self.winnerText:SetText(text)
	return self.winnerText:GetStringWidth()
end

function lootlog.LootEntry.prototype:Show()
	self.frame:Show()
end

function lootlog.LootEntry.prototype:Hide()
	self.frame:Hide()
end

function lootlog.LootEntry.prototype:SetBackgroundColor(r, g, b, a)
	self.frame:SetBackdropColor(r, g, b, a);
end

function lootlog.LootEntry.prototype:SetBorderColor(r, g, b)
	self.frame:SetBackdropBorderColor(r, g, b, 1);
end

function lootlog.LootEntry.prototype:SetWidth(width)
	self.frame:SetWidth(width)
end